package cn.iocoder.yudao.framework.region.bo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.region.PinyinUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.annotation.Nonnull;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 区域节点，包括省市区县信息
 *
 * @author 山野羡民（1032694760@qq.com）
 * @see cn.hutool.core.lang.tree.TreeUtil
 * @see cn.hutool.core.lang.tree.Tree;
 * @see cn.hutool.core.lang.tree.parser.DefaultNodeParser
 */
@Data
@NoArgsConstructor
@Accessors(chain = true)
public class RegionNode implements Serializable {
    public static final String CODE_CHINA = "000000";
    @JsonProperty("code")
    private String code;
    @JsonProperty("parentCode")
    private String parentCode;
    @JsonProperty("type")
    private Integer type;
    @JsonProperty("name")
    private String name;
    @JsonProperty("shortName")
    private String shortName;
    @JsonProperty("pinyin")
    private String pinyin;
    private String alphabet;
    private List<RegionNode> children;

    public String getCode() {
        return StrUtil.padAfter(code, 6, '0');
    }

    public String getParentCode() {
        return StrUtil.padAfter(parentCode, 6, '0');
    }

    @SuppressWarnings("unused")
    public String getAlphabet() {
        if (StrUtil.isBlank(alphabet)) {
            return PinyinUtils.toAlphabet(getName());
        }
        return alphabet;
    }

    @Nonnull
    public List<RegionNode> getChildren() {
        if (CollUtil.isEmpty(children)) {
            children = new ArrayList<>();
        }
        return children;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        RegionNode node = (RegionNode) o;
        return Objects.equals(code, node.code);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code);
    }

}
